const httpsReg = /^https:\/\//

export function createProxy(proxy: string) {
  if (!proxy) {
    return {}
  }

  const list = JSON.parse(proxy) as [string, string][]

  const rst = {}

  for (const [prefix, target] of list) {
    const isHttps = httpsReg.test(target)

    rst[prefix] = {
      target: target,
      changeOrigin: true,
      ws: true,
      rewrite: (path: string) => path.replace(new RegExp(`^${prefix}`), ''),
      ...(isHttps ? { secure: false } : {}),
    }
  }
  return rst
}
